package com.example.patentback.service.impl;

import com.example.patentback.dao.DocumentMapper;
import com.example.patentback.entity.Document;
import com.example.patentback.entity.PatentDocumentDto;
import com.example.patentback.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class DocumentServiceImpl implements DocumentService {

    @Autowired
    DocumentMapper documentMapper;

    @Override
    public Document insertDocument(PatentDocumentDto patentDocumentDto) {
        Document document = new Document();
        document.setUrl("document/1.txt");
        document.setField(patentDocumentDto.getField());
        document.setBackground(patentDocumentDto.getBackground());
        document.setIntroduce(patentDocumentDto.getIntroduce());
        documentMapper.insertUseGeneratedKeys(document);
        return document;
    }

    @Override
    public Document selectById(Long id) {
        Example example = new Example(Document.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        return documentMapper.selectByExample(example).get(0);
    }

    @Override
    public int deleteById(Long id) {
        Example example = new Example(Document.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        return documentMapper.deleteByExample(example);
    }
}
